package com.softtown.modules.sys.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softtown.common.exception.ErrorCode;
import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.common.utils.ConvertUtils;
import com.softtown.common.utils.DateUtils;
import com.softtown.common.utils.Result;
import com.softtown.modules.security.user.SecurityUser;
import com.softtown.modules.security.user.UserDetail;
import com.softtown.modules.sys.dao.SysMessageDao;
import com.softtown.modules.sys.dto.SysMessageDTO;
import com.softtown.modules.sys.dto.SysMessageUnreadDTO;
import com.softtown.modules.sys.entity.SysMessageEntity;
import com.softtown.modules.sys.service.SysMessageService;
import com.softtown.modules.sys.vo.SysMessageNoReadVO;
import com.softtown.modules.sys.vo.SysMessageVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 消息服务实现
 *
 * @author Dingyayong
 * @version v1.0
 * @create 2019-07-10 10:37
 **/
@Service
public class SysMessageServiceImpl extends BaseServiceImpl<SysMessageDao, SysMessageEntity> implements SysMessageService {
    @Override
    public PageInfo getMessageList(SysMessageDTO sysMessageDTO) {
        UserDetail userDetail = SecurityUser.getUser();
        Page page = PageHelper.startPage(sysMessageDTO.getPage(), sysMessageDTO.getPageSize());
        String beginDate = "";
        String endDate = "";
        if (sysMessageDTO.getStartTime() != null) {
            beginDate = DateUtils.dealDateFormat(sysMessageDTO.getStartTime());
        }
        if (sysMessageDTO.getEndTime() != null) {
            endDate = DateUtils.dealDateFormat(sysMessageDTO.getEndTime());
        }
        List<SysMessageEntity> sysMessageEntities = baseDao.selectByParam(sysMessageDTO.getParam(), beginDate, endDate, userDetail.getId());
        List<SysMessageVO> sysMessageVOList = ConvertUtils.sourceToTarget(sysMessageEntities, SysMessageVO.class);
        return ConvertUtils.pageInfoToTarget(page, sysMessageVOList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteMessage(Long messageId) {
        Result result = new Result<>();
        int flag = baseDao.removeById(SecurityUser.getUser().getId(), messageId);
        if (flag <= 0) {
            return result.error(ErrorCode.DELETE_FAILED);
        }
        return result;
    }

    @Override
    public Result updateReadMessage(Long id) {
        Result result = new Result<>();
        int successFlag = baseDao.updateByIdReadMessage(id);
        if (successFlag <= 0) {
            return result.error(ErrorCode.MARK_FAILED);
        }
        return result;
    }

    @Override
    public Result updateNotReadMessage(SysMessageUnreadDTO sysMessageUnreadDTO) {
        Result result = new Result<>();
        int successFlag = baseDao.updateByIdNotReadMessage(sysMessageUnreadDTO.getId());
        if (successFlag <= 0) {
            return result.error(ErrorCode.UNREAD_FAILED);
        }
        return result;
    }

    @Override
    public List<SysMessageNoReadVO> queryNoReadMessage() {
        List<SysMessageEntity> sysMessageEntities = baseDao.querySelectByNotRead(SecurityUser.getUser().getId());
        return ConvertUtils.sourceToTarget(sysMessageEntities, SysMessageNoReadVO.class);
    }
}

